package demo12.gumballmonitor;

import java.util.Random;
/**
 * 投入25分状态
 * */
public class HasQuarterState implements State {
	private static final long serialVersionUID = 2L;
	Random randomWinner = new Random(System.currentTimeMillis());
	GumballMachine gumballMachine;
 
	public HasQuarterState(GumballMachine gumballMachine) {
		this.gumballMachine = gumballMachine;
	}
  
	@Override
	public void insertQuarter() {
		System.out.println("你不能投入另一个25分");
	}
 
	@Override
	public void ejectQuarter() {
		System.out.println("已经退还25分");
		gumballMachine.setState(gumballMachine.getNoQuarterState());
	}
 
	@Override
	public void turnCrank() {
		System.out.println("得到糖果...");
		int winner = randomWinner.nextInt(10);//1-10
		if ((winner == 0) && (gumballMachine.getCount() > 1)) {
			gumballMachine.setState(gumballMachine.getWinnerState());
		} else {
			gumballMachine.setState(gumballMachine.getSoldState());
		}
	}

    @Override
	public void dispense() {
        System.out.println("没有糖果分配了");
    }
    
	@Override
	public String toString() {
		return "等待转动曲柄";
	}
}
